<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="机构名称" prop="tenantName">
        <el-select v-model="queryParams.tenantId" placeholder="请选择机构" clearable filterable>
          <el-option v-for="item in tenantList" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="模板名称" prop="templateName">
        <el-input
          v-model="queryParams.templateName"
          placeholder="请输入模板名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标题" prop="messageHeader">
        <el-input
          v-model="queryParams.messageHeader"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable
                        v-model="dateRange"
                        type="daterange"
                        value-format="yyyy-MM-dd"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:message:add']"
        >创建消息推送
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="messageList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" width="55" align="center">
        <template slot-scope="scope">{{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
          }}
        </template>
      </el-table-column>
      <el-table-column label="所属机构" align="center" prop="tenantName"/>
      <el-table-column label="模板编码" align="center" prop="templateCode"/>
      <el-table-column label="模板名称" align="center" prop="templateName" show-overflow-tooltip />
      <el-table-column label="标题" align="center" prop="messageHeader" show-overflow-tooltip />
      <el-table-column label="内容" align="center" prop="messageContent" show-overflow-tooltip/>
      <el-table-column label="触发行为" align="center" prop="triggeringBehavior">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.triggering_behavior" :value="scope.row.triggeringBehavior"/>
        </template>
      </el-table-column>
      <el-table-column label="推送渠道" align="center" prop="pushChannelName"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}:{h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="ordinaryMessageState">
        <template slot-scope="scope">
          <span v-if="scope.row.ordinaryMessageState === '0'">启用</span>
          <span v-else>禁用</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleDetail(scope.row)"
            v-hasPermi="['business:message:edit']"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:message:edit']"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:message:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改普通消息推送对话框 -->
    <add :show.sync="open" :data="form" :title="title" @success="getList" />
    <!-- 详情弹窗 -->
    <detail :show.sync="showDetail" :data="detail"/>
  </div>
</template>

<script>
import {listMessage, getMessage, delMessage, addMessage, updateMessage} from "@/api/messageCenter/ordinaryMessage";
import {listDepartment} from "@/api/sys-enterprise";
import Detail from "./components/detail.vue"
import Add from "./components/add.vue"
import {debounce} from "@/utils";

export default {
  name: "Message",
  dicts: ['triggering_behavior'],
  components: {
    Detail,
    Add
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 机构列表
      tenantList: [],
      // 总条数
      total: 0,
      // 普通消息推送表格数据
      messageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 详情弹窗
      showDetail: false,
      // 详情数据
      detail: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        templateName: null, // 模板名称
        messageHeader: null, // 标题
        createTime: null,
        tenantId: null // 机构id
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    this.getDepartmentList();
  },
  methods: {
    // 查询机构列表
    getDepartmentList() {
      listDepartment().then(response => {
        this.tenantList = response.data.map(item => {
          return {
            label: item.tenantName,
            value: item.tenantId
          }
        });
      });
    },
    /** 查询普通消息推送列表 */
    getList() {
      this.loading = true;
      listMessage(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.messageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ordinaryMessageId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form = {};
      this.open = true;
      this.title = "添加普通消息推送";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const ordinaryMessageId = row.ordinaryMessageId || this.ids
      getMessage(ordinaryMessageId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改普通消息推送";
      });
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      getMessage(row.ordinaryMessageId).then(response => {
        this.detail = response.data;
        this.showDetail = true;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ordinaryMessageIds = row.ordinaryMessageId || this.ids;
      this.$modal.confirm('是否确认删除此数据项？').then(function () {
        return delMessage(ordinaryMessageIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/message/export', {
        ...this.queryParams
      }, `message_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
